package com.ocom.webapi.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ocom.common.constant.WebApiServiceUrl;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.access.GroupVo;
import com.ocom.webapi.feign.AccessClientService;
import com.ocom.webapi.service.AccessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@PreAuthorize("@pms.hasRole('ROLE_WEB')" )
public class GroupInfoController {

    @Autowired
    private AccessClientService accessClientService;

    //权限组列表
    @PostMapping(WebApiServiceUrl.URL_AccessGroupPageOnShow)
    public Result merchPageGetFkConfig(@RequestBody JSONObject jsonObject){

        Result result=accessClientService.getZoneList(jsonObject.getLong("comId"));
        if (result.getCode()!=200){
            return result;
        }

        Result resulttGroupTimeList=accessClientService.getGroupTimeList(jsonObject.getLong("comId"),jsonObject.getString("areaNo"));
        if (resulttGroupTimeList.getCode()!=200){
            return result;
        }
        Map<String,Object> map= JSON.parseObject(JSON.toJSONString(resulttGroupTimeList.getData()),Map.class);
        map.put("zoneList",result.getData());
        map.put("comId",jsonObject.getLong("comId"));
        map.put("areaNo",jsonObject.getString("areaNo"));
        return ResultUtil.success(map);
    }

    //添加权限组（AccessGroupPage/addGroup）
    @PostMapping(WebApiServiceUrl.URL_AccessGroupPageAddGroup)
    public Result accessGroupPageAddGroup(@RequestBody JSONObject jsonObject){
        GroupVo groupVo=new GroupVo();
        groupVo.setCompanyID(jsonObject.getLong("comId"));
        groupVo.setGroupNo(jsonObject.getLong("groupNo"));
        groupVo.setGroupName(jsonObject.getString("groupName"));
        groupVo.setAreaNo(jsonObject.getString("areaNo"));
        return accessClientService.addAccessGroup(groupVo);
    }

    //修改权限组名称
    @PostMapping(WebApiServiceUrl.URL_AccessGroupPageModifyName)
    public Result accessGroupPageModifyName(@RequestBody JSONObject jsonObject){
        GroupVo groupVo=new GroupVo();
        groupVo.setCompanyID(jsonObject.getLong("comId"));
        groupVo.setGroupNo(jsonObject.getLong("groupNo"));
        groupVo.setGroupName(jsonObject.getString("groupName"));
        groupVo.setAreaNo(jsonObject.getString("areaNo"));
        return accessClientService.modifyGroupName(groupVo);
    }
}
